﻿/******************************************************************
** 文 件 名: Bu_Investigate_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-6-9 12:35:24
** 描    述: 现场考察编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.IO;
using System.Collections.Generic;
using Honestysoft.Baseframework.Model;
using System.Data.Common;


namespace Honestysoft.Financing.WebUI.Module.Project
{
	public partial class Bu_Investigate_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }
		
		private void BindControl()
		{
            QueryParameters query = new QueryParameters();
            OrderParameters order = new OrderParameters();
            order.Add("SortCode");
            DataTable dt = new Honestysoft.BaseObject.BaseBusinessLogic<BD_Project>().GetTable(query, order);
            dt.Columns.Add("disName");
            foreach (DataRow dr in dt.Rows)
            {
                dr["disName"] = dr["SN"] +" "+ dr["Name"];
            }
            ddlProject.DataSource = dt;
            ddlProject.DataTextField = "disName";
            ddlProject.DataValueField = "ID";
            ddlProject.DataBind();
            ddlProject.Items.Insert(0, new ListItem("", ""));
		}

        private void Bind()
        {
			Bu_Investigate obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Bu_Investigate>().GetEntity(Request.QueryString["ID"]);
                dtpInvDate.Text = obj.InvDate.ToString("yyyy-MM-dd");
                ddlProject.SelectedValue = obj.ProjectID;
                hidProjectID.Value = obj.ProjectID;
                ddlProject.Enabled = false;
                numGS.Text = obj.GS.ToString("0.00");
                numCZLJ.Text = obj.CZLJ.ToString("0.00");
                numTK.Text = obj.TK.ToString("0.00");
                numOutputValue.Text = obj.OutputValue.ToString("0.00");
                numP_ZF.Text = obj.P_ZF.ToString("0.00");
                numP_XX.Text = obj.P_XX.ToString("0.00");
                numP_GC.Text = obj.P_GC.ToString("0.00");
                dtpPlanStartDate.Text = obj.PlanStartDate.ToString("yyyy-MM-dd");
                dtpPlanEndDate.Text = obj.PlanEndDate.ToString("yyyy-MM-dd");
                dtpStartDate.Text = obj.StartDate.ToString("yyyy-MM-dd");
                dtpEndDate.Text = obj.EndDate.ToString("yyyy-MM-dd");
                txtDescription.Text = obj.Description;
            }
            else
            {
                dtpInvDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
            }
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(Globals.CurrentConnStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                Bu_Investigate obj = GetSubmitObject();
                
                //如果已经有项目简介，则拷贝到现场报告的目录里面
                Utils utils = new Utils();
                Sys_Attachment overview = utils.GetOverview(obj.ProjectID);
                Sys_Attachment newAtt = new Sys_Attachment();
                if (overview == null)
                {
                    newAtt = utils.CreateInvestigateBlank(obj.ID,UserLoginer.LoginUser.ID);
                }
                else
                {
                    File.Copy(Server.MapPath(Globals.Home + overview.Path + "/" + overview.FileName + "." + overview.Extension), Server.MapPath(Globals.Home + Utils.Reports_Dir_Investigate + "/" + obj.ID + "." + overview.Extension));
                    newAtt.CreateTime = DateTime.Now;
                    newAtt.Creator = UserLoginer.LoginUser.ID;
                    newAtt.DataID = obj.ID;
                    newAtt.Description = "";
                    newAtt.FileName = obj.ID;
                    newAtt.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    newAtt.Path = Utils.Reports_Dir_Investigate;
                    newAtt.StoreType = ((int)Honestysoft.Baseframework.Model.EnumItem.StoreType.Disk).ToString();
                    newAtt.Size = overview.Size;
                    newAtt.Extension = overview.Extension;
                }
                //如果当前是项目的最后一个项目考察，则更新项目的计划实际完成日期
                DateTime? maxDate = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetMaxInvDateByProject(obj.ProjectID);
                if (!maxDate.HasValue || (maxDate.HasValue && obj.InvDate >= maxDate.Value))
                {
                    BaseBusinessLogic<BD_Project> bllProject = new BaseBusinessLogic<BD_Project>(Globals.CurrentConnStringName);
                    BD_Project project = bllProject.GetEntity(obj.ProjectID);
                    project.PlanStart = obj.PlanStartDate;
                    project.PlanEnd = obj.PlanEndDate;
                    project.StartDate = obj.StartDate;
                    project.EndDate = obj.EndDate;
                    bllProject.UpdateEntity(project, transaction);
                }
                new BaseBusinessLogic<Bu_Investigate>(Globals.CurrentConnStringName).CreateEntity(obj, transaction);
                new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName).CreateEntity(newAtt, transaction);
                transaction.Commit();
                ScriptHelper.Window.AlertClose("保存成功！", true);
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！"+ex.Message);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {            
            try
            {
                Bu_Investigate obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<Bu_Investigate>().UpdateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Bu_Investigate GetSubmitObject()
        {
            Bu_Investigate obj = new Bu_Investigate();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
				obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            obj.ProjectID = hidProjectID.Value;
			if(dtpInvDate.Text.Trim().Length > 0){obj.InvDate = Convert.ToDateTime(dtpInvDate.Text);}
			if(numGS.Text.Trim().Length > 0){obj.GS = Convert.ToDecimal(numGS.Text);}
			if(numCZLJ.Text.Trim().Length > 0){obj.CZLJ = Convert.ToDecimal(numCZLJ.Text);}
			if(numTK.Text.Trim().Length > 0){obj.TK = Convert.ToDecimal(numTK.Text);}
			if(numOutputValue.Text.Trim().Length > 0){obj.OutputValue = Convert.ToDecimal(numOutputValue.Text);}
			if(numP_ZF.Text.Trim().Length > 0){obj.P_ZF = Convert.ToDecimal(numP_ZF.Text);}
			if(numP_XX.Text.Trim().Length > 0){obj.P_XX = Convert.ToDecimal(numP_XX.Text);}
			if(numP_GC.Text.Trim().Length > 0){obj.P_GC = Convert.ToDecimal(numP_GC.Text);}
			if(dtpPlanStartDate.Text.Trim().Length > 0){obj.PlanStartDate = Convert.ToDateTime(dtpPlanStartDate.Text);}
			if(dtpPlanEndDate.Text.Trim().Length > 0){obj.PlanEndDate = Convert.ToDateTime(dtpPlanEndDate.Text);}
			if(dtpStartDate.Text.Trim().Length > 0){obj.StartDate = Convert.ToDateTime(dtpStartDate.Text);}
			if(dtpEndDate.Text.Trim().Length > 0){obj.EndDate = Convert.ToDateTime(dtpEndDate.Text);}
            obj.Description = txtDescription.Text;
            return obj;
        }
	}
}